package cn.com.dhcc.ddi.task.service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.datasource.dao.TableColDao;
import cn.com.dhcc.ddi.datasource.vo.TableColVo;
import cn.com.dhcc.ddi.datasource.vo.TableInfoVo;
import cn.com.dhcc.ddi.task.dao.TaskTabDao;
import cn.com.dhcc.ddi.task.vo.TaskTab;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @日期：2016/11/15 16:26
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Service
public class TaskTabService extends BaseService {
    private static Logger logger = Log.getLogger(Log.LOGTYPE.DDI);
    @Autowired
    private TaskTabDao dao;

    @Autowired
    private TableColDao colDao;

    public List<TableInfoVo> getListBytaskId(String taskId) throws ServiceException {
        try {
            List <TableInfoVo> list = new ArrayList<TableInfoVo>();
            List<TableInfoVo> tabList = dao.getListBytaskId(taskId);
            for (TableInfoVo tab : tabList) {
                List<TableColVo> colList = colDao.getColsByTableId(tab.getId());
                tab.setTableColVos(colList);
                list.add(tab);
            }
            return list;
        } catch (DaoException e) {
            logger.error("获取表信息失败", e);
            throw new ServiceException("获取表信息失败", e);
        }
    }
    
    public List<TaskTab> getTaskTabByTaskId(String taskId) throws ServiceException {
    	try {
			return dao.getTaskTabByTaskId(taskId);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
    }
    
    /**
     * 更新过或插入任务表
     * @param table
     * @throws ServiceException
     */
    public void insertOrUpdateTaskTab(TaskTab table) throws ServiceException {
		try {
			CM.getDao().saveOrUpdateModuleByIdExecuteNull("添加或者修改任务", table);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

    /**
     * 根据任务id删除对应表
     * @param taskId
     * @return
     * @throws ServiceException
     */
    public int deleteByTaskId(String taskId) throws ServiceException {
    	try {
			return dao.deleteByTaskId(taskId);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
    }

}
